<?php

namespace app\api\controller;

use app\admin\model\xlzxinfo\Xlzxinfo;
use app\common\controller\Api;
use app\admin\model\video\Order;
use app\admin\model\consult\Order as ConsultOrder;
use app\common\model\User;
use think\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        
        // $list = \think\Db::name('mental_testlog')->paginate(10);
        // $this->success('addons',$list);
        // \addons\mental\model\Order::settle('20220727051943000000076729',0.05,'',3333,'pay');
        
        
        
        
        
        
        
        
    }

    /**
     * 定时查询已经开始还未结束的订单咨询，计算佣金
     * 未后台设置的订单
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function commission(){
        $ConsultOrderModel = new ConsultOrder();
        $order = $ConsultOrderModel->where(['status'=>2,'consult_type'=>1])->select();
        foreach ($order as $key => $value) {
            $starttime = strtotime($value['server_start_time']);
            // $time = time();
            // echo $value['server_start_time'].'####'.date('Y-m-d H:i:s')."<br/>";
            $minutes = $value['minutes']*60;
            $newTimestamp = date('Y-m-d H:i:s',$starttime+$minutes);

            // echo $newTimestamp."<br/>";
            if(time()>strtotime($newTimestamp) && empty($value['server_end_time'])){
                $ConsultOrderModel->ConsultCommission($value['id']);               
                //$endtime = date('Y-m-d H:i:s');
                $ConsultOrderModel->where(['id'=>$value['id']])->update(['server_end_time'=>$newTimestamp,'status'=>3]);
            }
            // $cle =  $time-$starttime; // 分数
            // echo $cle."<br/>";
            // $m =  floor($cle/60);
            // if($m>4){
            //     //计算佣金，然后更新状态 
            //     $ConsultOrderModel->ConsultCommission($value['id']);               
            //     $endtime = date('Y-m-d H:i:s');
            //     $ConsultOrderModel->where(['id'=>$value['id']])->update(['server_end_time'=>$endtime,'status'=>3]);
            // }
        }
        unset($value);
        die('完成');
    }

    /**
     * 后台手动关闭的订单，进行佣金返佣计算
     * @return void
     */
    public function closed_order_commission()
    {
        $ConsultOrderModel = new ConsultOrder();
        //已关闭，未返佣的订单
        $orderlist = $ConsultOrderModel->where(['status'=>5,'is_rebate'=>0,'effective_time'=>['<',time()]])->select();
        foreach ($orderlist as $k => $v) {
            //如果是兑换码订单，并且兑换码价格未设置则不进行返佣
            if($v['pay_type']=='code' && $v['exchange_price']=='0.00'){
                continue;
            }
            //存在用户的上级，推广订单佣金计算
            $user = \think\Db::name('user')->where('id', $v['user_id'])->where('status', 'normal')->find();
            if ($user) {
                //上下级推广佣金
                $parent_user = \think\Db::name('user')->where('id', $user['pid'])->where('status', 'normal')->find();
                if ($parent_user){
                    //分佣比例
                    if ($parent_user['commission_rate']>0){
                        $commission_rate1 = $parent_user['commission_rate'];
                    }else{
                        $commission_rate1 = Config::get('site.commission_rate');
                    }
                    $commission_rate_parent = bcMath($commission_rate1,100,'/');
                    //兑换码订单
                    if($v['pay_type']=='code'){
                        $price2 = bcMath($v['exchange_price'],$commission_rate_parent,'*');
                    }else{
                        $price2 = bcMath($v['price'],$commission_rate_parent,'*');
                    }
                    //文字订单
                    if($v['consult_type']==1){
                        $memo = '用户推广文字咨询分佣(关闭订单) 比例'.$commission_rate1.'% <br/>订单编号:'.$v['order_sn'];
                    }
                    //语音订单
                    if($v['consult_type']==2){
                        $memo = '用户推广语音咨询分佣(关闭订单) 比例'.$commission_rate1.'% <br/>订单编号:'.$v['order_sn'];
                    }
                    //给到余额
                    \app\common\model\User::money($price2,$parent_user['id'],$memo,5,2);
                    \think\Db::name('user')->where('id', $parent_user['id'])->setInc('cumulative_gain',$price2);
                }
            }

            //给咨询师分佣
            $commission_rate = Config::get('site.xlzx_commission_rate');
            if($v['pay_type']=='code'){
                $commission_price = bcMath($v['exchange_price'],$commission_rate/100,'*',2);
            }else{
                $commission_price = bcMath($v['price'],$commission_rate/100,'*',2);
            }
            $xlzxinfo = Xlzxinfo::where(['id'=>$v['xlzxinfo_id']])->find();
            //文字订单
            if($v['consult_type']==1){
                User::money($commission_price,$xlzxinfo['user_id'],'文字咨询订单(关闭订单) 佣金'.$commission_rate.'% <br/>咨询订单号:'.$v['id'].'<br/>订单编号'.$v['order_sn'],7,1);
            }
            //语音订单
            if($v['consult_type']==2){
                User::money($commission_price,$xlzxinfo['user_id'],'语音咨询订单(关闭订单) 佣金'.$commission_rate.'% <br/>咨询订单号:'.$v['id'].'<br/>订单编号'.$v['order_sn'],7,1);
            }

            //订单更改为已经完成分佣
            \app\admin\model\consult\Order::where(['pay_status'=>'paid','id'=>$v['id']])->update(['is_rebate'=>1]);

        }
        unset($v);
        die('完成');
    }

    /**
     * 后台自动完成的兑换码订单，并且有兑换码金额才返佣
     * @return void
     */
    public function dhm_order_commission()
    {
        $ConsultOrderModel = new ConsultOrder();
        //已完成的兑换订单，未分佣
        $order_list = $ConsultOrderModel->where(['status'=>3,'is_rebate'=>0,'exchange_price'=>['<>','0.00'],'pay_type'=>'code'])->select();
        foreach ($order_list as $k => $v) {
            //存在用户的上级，推广订单佣金计算
            $user = \think\Db::name('user')->where('id', $v['user_id'])->where('status', 'normal')->find();
            if ($user) {
                //上下级推广佣金
                $parent_user = \think\Db::name('user')->where('id', $user['pid'])->where('status', 'normal')->find();
                if ($parent_user){
                    //分佣比例
                    if ($parent_user['commission_rate']>0){
                        $commission_rate1 = $parent_user['commission_rate'];
                    }else{
                        $commission_rate1 = Config::get('site.commission_rate');
                    }
                    $commission_rate_parent = bcMath($commission_rate1,100,'/');
                    //兑换码订单

                    $price2 = bcMath($v['exchange_price'],$commission_rate_parent,'*');
                    //文字订单
                    if($v['consult_type']==1){
                        $memo = '用户推广文字咨询分佣(兑换码订单) 比例'.$commission_rate1.'% <br/>订单编号:'.$v['order_sn'];
                    }
                    //语音订单
                    if($v['consult_type']==2){
                        $memo = '用户推广语音咨询分佣(兑换码订单) 比例'.$commission_rate1.'% <br/>订单编号:'.$v['order_sn'];
                    }
                    //给到余额
                    \app\common\model\User::money($price2,$parent_user['id'],$memo,5,2);
                    \think\Db::name('user')->where('id', $parent_user['id'])->setInc('cumulative_gain',$price2);
                }
            }

            //给咨询师分佣
            $commission_rate = Config::get('site.xlzx_commission_rate');

            $commission_price = bcMath($v['exchange_price'],$commission_rate/100,'*',2);

            $xlzxinfo = Xlzxinfo::where(['id'=>$v['xlzxinfo_id']])->find();
            //文字订单
            if($v['consult_type']==1){
                User::money($commission_price,$xlzxinfo['user_id'],'文字咨询订单(兑换码订单) 佣金'.$commission_rate.'% <br/>咨询订单号:'.$v['id'].'<br/>订单编号'.$v['order_sn'],7,1);
            }
            //语音订单
            if($v['consult_type']==2){
                User::money($commission_price,$xlzxinfo['user_id'],'语音咨询订单(兑换码订单) 佣金'.$commission_rate.'% <br/>咨询订单号:'.$v['id'].'<br/>订单编号'.$v['order_sn'],7,1);
            }

            //订单更改为已经完成分佣
            \app\admin\model\consult\Order::where(['pay_status'=>'paid','id'=>$v['id']])->update(['is_rebate'=>1]);
        }
        die('完成');

    }
    
}
